<template>
	<view class="searchRequirements">
		<view class="topSearch">
			<view class="topSearch_view">
				<image class="topSearch_view_img" src="/static/img/index/61.png" mode="widthFix"></image>
				<input @confirm="data.obj.pageNo = 1,requstList()" v-model="data.obj.title" class="topSearch_view_input"
					type="text" placeholder="搜索" />
			</view>
			<picker :range="data.topArr" range-key="name" @change="distanceChange">
				<view class="view">
					<image src="/static/img/my/distance.png" mode="widthFix"></image>
					{{data.obj.distance?data.topArr[data.topArr.map(item=>item.id).indexOf(data.obj.distance)].name:' 距离 '}}
				</view>
			</picker>
			<picker :range="data.productArr" range-key="name" @change="productTypeChange">
				<view class="view">
					<image src="/static/img/my/switch.png" mode="widthFix"></image>
					{{data.obj.productType?data.productArr[data.productArr.map(item=>item.id).indexOf(data.obj.productType)].name:' 类型 '}}
				</view>
			</picker>
		</view>
		<scroll-view @refresherrefresh="refresherrefresh" @scrolltolower="scrolltolower"
			:refresher-triggered="data.refresherBool" :refresher-enabled="true" scroll-y="true">
			<view class="list">
				<view @click="handeClick(item)" class="listItem" v-for="(item,index) in data.result.list" :key="index">
					<!-- 	<view class="top">
						<text></text>
						<text>已上架</text>
					</view> -->
					<view class="goods">
						<view :class="item.productType.value == 'DEMAND'?'viewRed':'viewBlue'">
							<!-- <text>{{item.productType == 'OFFLINE'?'线下':'线上'}}</text> -->
							<text>{{item.productType.name}}</text>
							<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item.attachmentIds[0]"
								mode="aspectFill"></image>
						</view>
						<view class="right">
							{{item.title}}
							<text>{{item.content}}</text>
							<view class="priceText">
								<text>{{item.price.split('.')[0]}}</text>
								<text>.{{item.price.split('.')[1]}}</text>
							</view>
						</view>
					</view>
					<text>{{item.distance}}km</text>
					<!-- <text>({{item.sellType == 'OFFLINE'?'线下':'线上'}})</text> -->

				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup lang="ts">
	import NavBar from '@/components/navBar.vue'
	import { reactive } from 'vue';
	import { onLoad } from '@dcloudio/uni-app'
	import { queryMapVpsDemand, getLat } from '@/utils/api/map'
	import {
			setDialogView
		} from '@/utils/dialogView.ts'
	const data = reactive<any>({
		httpUrl: getApp().globalData.baseUrl,
		result: {},
		obj: { pageNo: 1, pageSize: 10, title: '', distance: 0 },
		topArr: [{ name: '不限', id: 0 }, { name: '1km', id: 1 }, { name: '5km', id: 5 }, { name: '10km', id: 10 }, { name: '20km', id: 20 }, { name: '30km', id: 30 }, { name: '50km', id: 50 }],
		productArr: [{ name: '推荐', id: '' }, { name: '需求', id: 'DEMAND' }, { name: '服务', id: 'SERVICE' }, { name: '商品', id: 'PRODUCT' }],
	})

	onLoad(({ city }) => {
		// #ifdef APP-PLUS
		plus.geolocation.getCurrentPosition(
			position => {
				// 成功获取位置
				console.log('用户同意')
			},
			error => {
				console.log('用户拒绝授权位置')
				getLat({ data: { name: city } }).then((res) => {
					data.obj.latitude = res.data.lat;
					data.obj.longitude = res.data.lng;
					requstList()
				})
			}
		);
		// #endif
	})
	const distanceChange = (e) => {
		data.obj.distance = data.topArr[e.detail.value].id;
		data.obj.pageNo = 1;
		requstList()
	}
	const productTypeChange = (e) => {
		data.obj.productType = data.productArr[e.detail.value].id;
		data.obj.pageNo = 1;
		requstList()
	}
	const requstList = (i = 0) => {
		if (!i) {
			uni.showLoading({
				title: '加载中...',
				mask: true
			})
		}

		if (data.obj.pageNo == 1) {
			data.result.list = [];
		}
		console.log(data.obj)
		queryMapVpsDemand({ data: data.obj }).then((res) => {
			res.data.list.map((item) => {
				item.price = item.price + ''
				if (item.price.indexOf('.') == -1) {
					item.price = item.price + '.00'
				}
				if (item.attachmentIds) {
					item.attachmentIds = item.attachmentIds.split(',')
				}
			})
			data.refresherBool = false;
			res.data.list = [...data.result.list, ...res.data.list];
			console.log(res.data)
			data.result = res.data;
			if (!i) {
				uni.hideLoading()
			}
		})
	}
	uni.$off('searchRequirements')
	uni.$on('searchRequirements', () => {
		data.obj.pageNo = 1;
		requstList(1)
	})
	setDialogView('ACCESS_FINE_LOCATION').then(res=>{
		if(res == 1){
			uni.getLocation({
				type: 'gcj02',
				success: function (res) {
					data.obj.latitude = res.latitude;
					data.obj.longitude = res.longitude;
					requstList()
				}
			});
		}
	})
	

	const refresherrefresh = () => {
		data.refresherBool = true;
		data.obj.pageNo = 1;
		requstList()
	}

	const scrolltolower = () => {
		if (data.result.total != data.result.list.length) {
			data.obj.pageNo += 1;
			requstList()
		}
	}
	const handeClick = (item) => {
		if (item.productType.value == 'DEMAND') {
			uni.navigateTo({
				url: '/pages/requirementDetails/index?id=' + item.id + `&longitude=${data.obj.longitude}&latitude=${data.obj.latitude}`
			})
		} else {
			uni.navigateTo({
				url: '/pages/lifestyleProducts/index?id=' + item.id + '&productType=' + item.productType.value
			})
		}

	}
</script>

<style lang="scss">
	.searchRequirements {
		height: 100vh;
		background: #F4F6FA;
		display: flex;
		flex-direction: column;
	}

	.list {
		>view:nth-of-type(1) {
			margin-top: 0;
		}
	}

	.listItem {
		padding: 20rpx;
		margin-top: 24rpx;
		background: #fff;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		>text {
			font-size: 22rpx;
			color: #595959;
		}

		>.top {
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #595959;

			text {
				color: #FF8C00;
			}

			padding: 20rpx 0;
		}

		>.goods {
			display: flex;
			align-items: center;
			flex: 1;

			.viewRed {
				position: relative;
				width: 140rpx;
				height: 140rpx;
				border-radius: 24rpx;
				background: #ccc;
				margin-right: 16rpx;

				text {
					width: 72rpx;
					line-height: 40rpx;
					text-align: center;
					font-size: 20rpx;
					color: #FFF5E2;
					display: inline-block;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 5;
					box-shadow: inset -1rpx -1rpx 4rpx 0rpx rgba(255, 234, 160, 0.3);
					border-radius: 24rpx 0 24rpx 0;
					background: linear-gradient(223deg, #ffb3b3 0%, #ffb3b3 100%);
				}

				image {
					width: 100%;
					height: 100%;
					border-radius: 24rpx;
				}
			}

			.viewBlue {
				position: relative;
				width: 140rpx;
				height: 140rpx;
				border-radius: 24rpx;
				background: #ccc;
				margin-right: 16rpx;

				text {
					width: 72rpx;
					line-height: 40rpx;
					text-align: center;
					font-size: 20rpx;
					color: #FFF5E2;
					display: inline-block;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 5;
					box-shadow: inset -1rpx -1rpx 4rpx 0rpx rgba(255, 234, 160, 0.3);
					border-radius: 24rpx 0 24rpx 0;
					background: linear-gradient(223deg, #66c2ff 0%, #66c2ff 100%);
				}

				image {
					width: 100%;
					height: 100%;
					border-radius: 24rpx;
				}
			}

			.right {
				flex: 1;
				width: 0;
				font-size: 32rpx;
				display: flex;
				flex-direction: column;

				>text {
					color: #595959;
					font-size: 24rpx;
					margin-top: 8rpx;
				}
			}
		}

		>.bot {
			margin-top: 24rpx;
			border-top: 2rpx solid #F4F6FA;
			display: flex;
			justify-content: flex-end;
			padding: 24rpx 0;

			text {
				width: 140rpx;
				line-height: 64rpx;
				text-align: center;
				border-radius: 32rpx;
				margin-left: 16rpx;
			}

			text:nth-of-type(1) {
				color: #595959;
				border: 2rpx solid #D9D9D9;
			}

			text:nth-of-type(2) {
				color: #6B64D3;
				border: 2rpx solid #6B64D3;
			}

		}
	}

	scroll-view {
		padding: 24rpx 20rpx;
		flex: 1;
		overflow: auto;
	}

	.topSearch {
		padding: 15rpx 20rpx;
		display: flex;
		background: #fff;
		align-items: center;
		flex-direction: row;

	}

	.topSearch_view_img {
		width: 35rpx;
		margin-right: 15rpx;
	}

	.topSearch_view_input {
		flex: 1;
		// color: #ccc;
	}

	.topSearch_view {
		flex: 1;
		padding: 11rpx 24rpx;
		border-radius: 64rpx;
		display: flex;
		align-items: center;
		border-radius: 64rpx;
		border: 2rpx solid #F4F6FA;
		flex-direction: row;
		position: relative;
		margin-right: 20rpx;

	}

	.view {
		width: 120rpx;
		height: 90rpx;
		display: flex;
		align-items: center;
		border-radius: 58rpx;
		justify-content: center;

		image {
			width: 24rpx;
			margin-right: 8rpx;
		}
	}
</style>